package org.example;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


public class JdkDynamicProxyExample {
    public static void main(String[] args) {
        Man man = new Man();
        People people= (People)Proxy.newProxyInstance(
                People.class.getClassLoader(),
                new Class[]{People.class},//Man.class.getInterfaces(),要实现类去调用这个方法，原接口调用为空数组。或者直接新建一个数组
                new SayHelloInvocationHandler(man));
        people.sayHello();
    }

}
class SayHelloInvocationHandler implements InvocationHandler{
    Object object;
    public SayHelloInvocationHandler(Object obj){
        object=obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("执行代码真正对象执行前的逻辑");
        Object res=method.invoke(object,args);
        System.out.println("执行代码真正对象执行后的逻辑");
        return res;
    }
}
class Man implements People{
    @Override
    public void sayHello() {
        System.out.println("hello,我是真男人");
        System.out.println("hello,我是真男人");
    }
}
interface People {
    void sayHello();
}